<template>
	<div class="system-menu-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="dialogFormRef" :model="state.ruleForm" :rules="state.ruleRules" size="default" label-width="80px">
				<el-row :gutter="35">
					
					<el-col>
						<el-form-item label="分类图标">
							<Uploadimg v-model="state.ruleForm.icon" :value="state.ruleForm.icon"   @upload="onUploadvedio"/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="分类名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入分类名称" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序" prop="sort">
							<el-input-number v-model="state.ruleForm.sort" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					
					
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemMenuForm">
import { defineAsyncComponent, reactive, onMounted, ref ,unref,nextTick} from 'vue';
import { save,edit } from '/@/api/Categories/index';
import { ElMessageBox, ElMessage } from 'element-plus';
import { handleTree } from '/@/utils';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const Uploadimg = defineAsyncComponent(() => import('/@/components/Upload/singleImage.vue'));


 const onUploadvedio = (data:any) => {
	console.log('打印图片',data)

	state.ruleForm.icon = data;
};
// 定义变量内容
const dialogFormRef = ref();
const state = reactive({
	ruleForm: {
		// id:0,
		name: '', // 菜单名称
		sort:1,// 菜单排序
		icon:''
	},
	menuData: [] , // 上级菜单数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	// 表单校验
	ruleRules: {
		name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
		permission: [{ required: true, message: '权限标识', trigger: 'blur' }],
	},
	loading: false,
});

// 打开弹窗
const openDialog = (type: string, row?: any) => {
	if (type === 'edit') {
		// 实际请走接口
		state.ruleForm = row;
		
		state.dialog.title = '修改';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		nextTick(() => {
			dialogFormRef.value.resetFields();
			initForm();
		});
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
	
};
// 关闭弹窗
const closeDialog = () => {
	initForm();
	state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {

	const formWrap = unref(dialogFormRef) as any;
	if (!formWrap) return;
	formWrap.validate((valid: boolean) => {
		if (valid) {
			state.loading = true;
			
			if(state.dialog.submitTxt == '新 增') {
				save(state.ruleForm).then(() => {
					ElMessage.success('保存成功');
					state.loading = false;
					closeDialog(); // 关闭弹窗
					emit('refresh');
				}).finally(() => {
					state.loading = false;
				});
				return
			}
			edit(state.ruleForm).then(() => {
				ElMessage.success('修改成功');
				state.loading = false;
				closeDialog(); // 关闭弹窗
				emit('refresh');
			}).finally(() => {
					state.loading = false;
				});

		}
	})
};
// 页面加载时
onMounted(() => {
	
});


// 表单初始化，方法：`resetFields()` 无法使用
const initForm = () => {
	state.ruleForm.id = 0;
	state.ruleForm.sort = 1;
	state.ruleForm.name = '';
	state.ruleForm.icon = ''
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
